<?php

class ReportForm extends Zend_Form{
	public function __construct($options = null)
	{
		parent::__construct($options);
		// set the form decorator path
		$this->addPrefixPath('Vmg_Form_Decorator', 'Vmg/Form/Decorator', 'decorator');

		// get registry
		$registry = Zend_Registry::getInstance();
		$translator = $registry->get('Zend_Translate');
		$reporting_config = $registry->get('reporting_config');
		$access_levels = $registry->get('access_level_config');

		// set name and id
		$this->setAttrib('name','reportForm');
		$this->setAttrib('id', 'reportForm');


		// salesman combo box
		$salesmans = new Zend_Form_Element_Select('salesmanReportForm');
		$salesmans->setLabel('Salesman');
		$salesmans->setDecorators(array('SpanTwo'));

		$userType = $access_levels->mobishop_level;
		$users = new Users();
		$select = $users->select()->from($users, array('id', 'name'))
       			->where('type_id in (select id from vmg_user_type where access_level <' . $userType . ' and access_level >= ' . ($userType -1) . ' and access_level <= ' . $access_levels->salesman_level . ')');
		$rows = $users->fetchAll($select);
		if(count($rows) >= 1){
			foreach($rows as $row){
				$salesmans->addMultiOption($row->id, $row->name);
			};
		}

		// salesman combo box
		$agents = new Zend_Form_Element_Select('agentReportForm');
		$agents->setLabel('Agent');
		$agents->setDecorators(array('SpanTwo'));


		/*$users = new Users();
		$select = $select = $users->getAdapter()->select()
								->from(array('u' => 'vmg_user'), array('u.id as agent_id', 'u.name as agent_name'))
					            ->joinleft(array('t' => 'vmg_user_type'),
					                        'u.type_id = t.id', array('type_name'))
					            ->where('t.access_level = ' . $access_levels->mobishop_level);
		$rows = $users->getAdapter()->fetchAll($select);
		foreach($rows as $row){
			$agents->addMultiOption($row['agent_id'], $row['agent_name']);
		}*/

		// year combo box
		$year = new Zend_Form_Element_Select('yearReportForm');
		$year->setLabel("Year/Month");
		$year->setDecorators(array('Composite'));

		$current_year = date('Y');
		for($i = $reporting_config->start_year; $i <= $current_year; $i++){
			$year->addMultiOption($i, $i);
		}
		$year->setValue($current_year);

		// month combo box
		$month = new Zend_Form_Element_Select('monthReportForm');
		$month->setLabel("");
		$month->setDecorators(array('Composite'));

		$month->addMultiOption(0, "January");
		$month->addMultiOption(1, "February");
		$month->addMultiOption(2, "March");
		$month->addMultiOption(3, "April");
		$month->addMultiOption(4, "May");
		$month->addMultiOption(5, "June");
		$month->addMultiOption(6, "July");
		$month->addMultiOption(7, "August");
		$month->addMultiOption(8, "September");
		$month->addMultiOption(9, "October");
		$month->addMultiOption(10, "November");
		$month->addMultiOption(11, "December");

		$month->setAttrib('onchange', 'populate_days(document.reportForm);');
		$current_month = date("m");
		$month->setValue($current_month-1);

		// start day combo box
		$start_day = new Zend_Form_Element_Select("startDayReportForm");
		$start_day->setLabel("Start day/hour");
		$start_day->setDecorators(array('Composite'));
		for($i = 1 ; $i < date("t", $current_month); $i++){
			$start_day->addMultiOption($i, $i);

		}
		$start_day->setValue(date("d"));
		// start hour
		$start_hour = new Zend_Form_Element_Select("startHourReportForm");
		$start_hour->setLabel("");
		$start_hour->setDescription("hour");
		$start_hour->setDecorators(array('Composite'));
		for($i=0; $i < 24; $i++){
			$start_hour->addMultiOption($i, $i);
		}


		// end day combo box
		$end_day = new Zend_Form_Element_Select("endDayReportForm");
		$end_day->setLabel("End day/hour");
		$end_day->setDecorators(array('Composite'));
		for($i = 1 ; $i < date("t", $current_month); $i++){
			$end_day->addMultiOption($i, $i);

		}
		$end_day->setValue(date("d"));
		$end_hour = new Zend_Form_Element_Select("endHourReportForm");
		$end_hour->setLabel("");
		$end_hour->setDescription("hour");
		$end_hour->setDecorators(array('Composite'));
		$end_hour->setValue(date('H'));
		for($i=0; $i < 24; $i++){
			$end_hour->addMultiOption($i, $i);
		}
		$submit = new Zend_Form_Element_Submit('submitReportForm');
		$submit->setAttrib('id', 'submitReportForm');
		$submit->setValue('Show report');
		$submit->setLabel('');
		$submit->setDecorators(array('Composite'));

		$export = new Zend_Form_Element_Submit("exportReportForm");
		$export->setAttrib('id', 'exportReportForm');
		$export->setValue('Export data');
		$export->setLabel('');
		$export->setDecorators(array('Composite'));

		$this->setTranslator($translator);
		$this->addElements(array($salesmans, $agents, $year, $month, $start_day, $start_hour, $end_day, $end_hour, $submit, $export));
	}
}
?>